/*
 * Copyright 2007 The semap project development team.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 * Created by myn at 21.08.2007 16:53:26.
 *
 * This class is a handler of the GET command.
 *
 * $Id: GetCmdProc.java 421 2007-08-22 11:43:11Z Mike.Navern $
 */
package com.google.code.contactserv;

import java.util.Iterator;
import java.util.List;

import com.google.code.contactserv.model.Contact;
import com.google.code.contactserv.model.PropertyHolder;
import com.google.code.contactserv.service.ContactService;

/**
 * @author myn
 *
 */
public class GetCmdProc extends CommandProcessor {

    /**
     * @param srv
     * @param line
     */
    public GetCmdProc(ContactService srv, List<PropertyHolder> lst) {
        super(srv, lst);
    }

    /* (non-Javadoc)
     * @see com.google.code.contactserv.CommandProcessor#doWork()
     */
    @Override
    public void doWork(String uri) {
        if (srv.exist(uri)) {
            outputContact(srv.get(uri), lang);
        }
    }

    /**
     * Prints specified fields of contact on stdout.
     */
    private void outputProperties(String title, List<String> lst) {
        for (String aLst : lst) {
            System.out.println(title + ": " + aLst);
        }
    }

    /**
     * Prints specified fields of contact on stdout.
     */
    private void outputContact(Contact contact, String lang) {
        System.out.println(contact.getUri());

        for (Iterator<PropertyHolder> i = lst.iterator(); i.hasNext();) {
            PropertyHolder holder = (PropertyHolder) i.next();
            outputProperties(holder.getName(), 
                    contact.getProps(holder.getProperty(), lang));
        }
    }
}
